#pragma once
#include "OgreStableHeaders.h"
#include "OgreExMessageHandlerBase.h"
#include "OgreExMessageHandler.h"
#include "OgreExAddress.h"
#include "OgreExSafeQueue.h"
namespace OgreEx
{
	class IMessage;
	class HandlerThreadBase;
	class IPort  : public Ogre::GeneralAllocatedObject
	{
	public:
		IPort() {}
		virtual ~IPort();
		
		__DeclareAddress(IPort);
	public:
		virtual void send(IMessage* msg) = 0;
		virtual void post(IMessage* msg) = 0;
		virtual void handle(IMessage* msg) = 0;
		
		template<class ValueType>
		void registerHandler( void (IPort::*HandlerFunction)(const ValueType &message) );

	protected:
		//SafeQueue<IMessage*>* mMsgQueue;
		typedef queue<MessageHandlerBase*> HandlerQueue;
		queue<MessageHandlerBase*> mHandlerQueue;
	};

	template <class ValueType>
	void IPort::registerHandler(void (OgreEx::IPort::* handlerFunction)(const ValueType &))
	{
		MessageHandlerBase* handler = new MessageHandler<IPort,ValueType>(this,handlerFunction);
		mHandlerQueue.push(handler);

	}
}